<?php
 
if (!defined('IN_PHPC')) {
	die("Hacking attempt");
}
global $noNavbar;
$noNavbar = true;

require_once($phpc_root_path . 'includes/event_form.php');
require_once($phpc_root_path . 'admin/cake13/app/vendors/calendar/generate_invoices_inc.php');
require_once($phpc_root_path . 'admin/cake13/app/vendors/calendar/generate_invoices_local_inc.php');

function trip_estimate_submit(){
	global $calendar_name, $day, $month, $year, $db, $vars, $config,
	       $phpc_script, $CANCELED, $OPERATION, $EVENT_TYPE_BOOKING, $MEMBER_PLANS;
    
    $error = Array();           

	if(empty($vars['day'])) $error[] =  (_('No day was given.'));

	if(empty($vars['month'])) $error[] =  (_('No month was given.'));

	if(empty($vars['year'])) $error[] = (_('No year was given'));

	if(isset($vars['starthour'])) {
              $hour = $vars['starthour'];
	} else {
              $error[] = (_('No hour was given.'));
    }
    
	if(isset($vars['endhour'])) {
              $endhour = $vars['endhour'];
	} else {
              $error[] = (_('No end hour was given.'));
    }

	if(isset($vars['num_km_estimate'])){
		$num_km_estimate = $vars['num_km_estimate'];
        if (is_numeric($num_km_estimate)){
            if ($num_km_estimate <= 1){
                $error[] = (_('Number of km must be greater than 1.'));
            }
        } else {
            $error[] = (_('Number of km must be a number.'));
        }
	}else $error[] = (_('Must estimate number of km.'));

	if(isset($vars['bookable']))
		$bookable = $vars['bookable'];
	else $error[] = (_('No vehicle was given.'));
	if ($bookable == 0 || $bookable == -1){
		$error[] = (_('No vehicle was selected.'));
	}
	
	if(isset($vars['endday']))
		$end_day = $vars['endday'];
	else $error[] = (_('No end day was given'));
    
	if(isset($vars['endmonth']))
		$end_month = $vars['endmonth'];
	else $error[] = (_('No end month was given'));

	if(isset($vars['endyear']))
		$end_year = $vars['endyear'];
	else $error[] = (_('No end year was given'));
	
	$startArray = split(":", $hour );
	$endArray = split(":", $endhour );
	
	$startstamp = mktime($startArray[0], $startArray[1], 0, $month, $day, $year);

	$endstamp = mktime($endArray[0], $endArray[1], 0, $end_month, $end_day, $end_year);
    if($endstamp < $startstamp) {
       $error[] = (_('The start of the event cannot be after the end of the event.'));
    }    

    if (count($error) > 0){
        return Array('error' => true, 'out' => $error);
    }
    
	$query= "select * from ".SQL_PREFIX."bookables where id=" . $bookable;

	$result = $db->Execute($query)
                or db_error("error getting bookable", $query);
    $bookableRow = $result->FetchRow(); 
    
	$query= "select * from ".SQL_PREFIX."billings order by year desc, month desc limit 1";

	$result = $db->Execute($query)
                or db_error("error getting billing", $query);
    $billing = $result->FetchRow();    

    $plan = $MEMBER_PLANS['HIGH'];    

    $row = array('hourly_rate_low' => $bookableRow['hourly_rate_low'],
        'hourly_rate_high' => $bookableRow['hourly_rate_high'],
        'is_flat_daily_rate' => $bookableRow['is_flat_daily_rate'],
        'daily_rate_low' => $bookableRow['daily_rate_low'],
        'daily_rate' => $bookableRow['daily_rate'],
        'rate_cutoff' => $bookableRow['rate_cutoff'],
        'km_rate_low' => $bookableRow['km_rate_low'],
        'km_rate_med' => $bookableRow['km_rate_med'],
        'km_rate_high' => $bookableRow['km_rate_high'],
        'distance' => $num_km_estimate,
        'name' => $bookableRow['name'],
        'starttime' => $year . '-' . $month . '-' . $day . ' ' . $hour,
        'endtime' => $end_year . '-' . $end_month . '-' . $end_day . ' ' . $endhour,
        'eventtype' => 1,
        'year' => $year,
        'month' => $month,
        'day' => $day,
        'end_year' => $end_year,
        'end_month' => $end_month,
        'end_day' => $end_day,
        'hour' => $hour,
        'minute' => $minute,
        'end_hour' => $endhour,
        'end_minute' => $endminute,
        );
    
 
	$subtotals = Array();
	initializeSubtotals($subtotals);
	
    $invRow = getBookingInvoiceRow($num_km_estimate, $billing, $row, $plan, $subtotals);
    
    return Array('error' => false, 'out' => $invRow);
}


function trip_estimate() {
    global $vars;
    $result = null;
    if ( isset ($vars['subaction']) && $vars['subaction'] == "trip_estimate_submit"){
        $ret = trip_estimate_submit();
        if ($ret['error']){
            $l = tag("ul");
            foreach($ret['out'] as $e){
                $l->add( tag('p',$e) );
            }
            $result = tag("div", $l);
        } else {
            $invRow = $ret['out'];
            $result = tag("div",
                    "The estimated cost for your trip using the " . $invRow['comment'] . " is: $" . number_format($invRow['time_charge'] + $invRow['km_charge'], 2) . " (time charge: $" . number_format($invRow['time_charge'], 2) . ", distance charge $". number_format($invRow['km_charge'], 2) .")");
        }
    }
    if ($result != null){
        $out = tag("div", $result, event_form());
    } else {
        $out = tag("div", event_form());
    }
    return $out;
}

?>